/* 
 * Copyright [2017] [Alex/LiBo(libo2dev.aliyun.com/alex.link@foxmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.bmsp.record;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

import org.apache.commons.lang.StringUtils;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

/**  
 * @Title RecordSupport.java  
 * @Package com.hyts.bmsp.record  
 * @Description DB数据库可以支持的类操作服务 ：为很不错的首选方法
 * @author LiBo/Alex  
 * @date 2017年12月24日  
 * @version V1.0  
 */
public abstract class RecordDaoSupport<PO,BO> extends AbstractQueryDao implements AbstractRecordDao<PO,BO>{

	/* (非 Javadoc)  
	 * <p>Title: insert</p>  
	 * <p>Description: 插入数据信息对象 </p>  
	 * @param po
	 * @return  
	 * @see com.hyts.bmsp.record.AbstractRecordDao#insert(java.lang.Object)  
	 */  
	@Override
	public int insert(PO po) {
		if(Objects.isNull(po) || po instanceof BaseModel)
			return 0;
		BaseModel model = (BaseModel)po;
		Record poRecord = new Record().setColumns((model).getNameAndValueMap());
		return Db.save(model.getTableName(), poRecord)?1:0;
	}

	/* (非 Javadoc)  
	 * <p>Title: insert</p>  
	 * <p>Description: </p>  
	 * @param pos
	 * @return  
	 * @see com.hyts.bmsp.record.AbstractRecordDao#insert(java.lang.Object[])  
	 */  
	@Override
	public int insert(PO... pos) {
		if(Objects.isNull(pos) || pos.length == 0)
			return 0;
		Record poRecord = new Record();
		String tableName = ((BaseModel)pos[0]).getTableName();
		int batchSize = ((BaseModel)pos[0]).getBatchSize();
		List<Record> recordList = new LinkedList<Record>();
		Arrays.stream(pos).forEach(po->{
			poRecord.setColumns(((BaseModel)po).getNameAndValueMap());
			recordList.add(poRecord);
			poRecord.clear();
		});
		return Db.batchSave(tableName,recordList, batchSize).length;
	}

	/* (非 Javadoc)  
	 * <p>Title: update</p>  
	 * <p>Description: </p>  
	 * @param po
	 * @return  
	 * @see com.hyts.bmsp.record.AbstractRecordDao#update(java.lang.Object)  
	 */  
	@Override
	public int update(PO po) {
		if(Objects.isNull(po) || po instanceof BaseModel)
			return 0;
		BaseModel model = (BaseModel)po;
		Record poRecord = new Record().setColumns((model).getNameAndValueMap());
		return Db.update(model.getTableName(),poRecord)?1:0;
	}

	/* (非 Javadoc)  
	 * <p>Title: update</p>  
	 * <p>Description: </p>  
	 * @param pos
	 * @return  
	 * @see com.hyts.bmsp.record.AbstractRecordDao#update(java.lang.Object[])  
	 */  
	@Override
	public int update(PO... pos) {
		if(Objects.isNull(pos) || pos.length == 0)
			return 0;
		Record poRecord = new Record();
		String tableName = ((BaseModel)pos[0]).getTableName();
		int batchSize = ((BaseModel)pos[0]).getBatchSize();
		List<Record> recordList = new LinkedList<Record>();
		Arrays.stream(pos).forEach(po->{
			poRecord.setColumns(((BaseModel)po).getNameAndValueMap());
			recordList.add(poRecord);
			poRecord.clear();
		});
		return Db.batchUpdate(tableName,recordList, batchSize).length;
	}

	/* (非 Javadoc)  
	 * <p>Title: deleteById</p>  
	 * <p>Description: </p>  
	 * @param id
	 * @return  
	 * @see com.hyts.bmsp.record.AbstractRecordDao#deleteById(java.lang.String)  
	 */  
	@Override
	public int deleteById(BO bo) {
		if(Objects.isNull(bo) || bo instanceof BaseModel)
			return 0;
		BaseModel model = (BaseModel)bo;
		return Db.deleteById(model.getTableName(),model.getId()) ? 1:0;
	}

	/* (非 Javadoc)  
	 * <p>Title: deleteByCondition</p>  
	 * <p>Description: </p>  
	 * @param bo
	 * @return  
	 * @see com.hyts.bmsp.record.AbstractRecordDao#deleteByCondition(java.lang.Object)  
	 */  
	@Override
	public int deleteByCondition(BO bo) {
		if(Objects.isNull(bo) || bo instanceof BaseModel)
			return 0;
		BaseModel model = (BaseModel)bo;
		Record poRecord = new Record().setColumns((model).getNameAndValueMap());
		return Db.delete(model.getTableName(),poRecord)?1:0;
	}

	/* (非 Javadoc)  
	 * <p>Title: selectById</p>  
	 * <p>Description: </p>  
	 * @param id
	 * @return  
	 * @see com.hyts.bmsp.record.AbstractRecordDao#selectById(java.lang.String)  
	 */  
	@Override
	public Record selectById(BO bo) {
		if(Objects.isNull(bo) || bo instanceof BaseModel)
			return null;
		BaseModel model = (BaseModel)bo;
		return Db.findById(model.getTableName(), model.getId());
	}

	/* (非 Javadoc)  
	 * <p>Title: selectByConditionToList</p>  
	 * <p>Description: </p>  
	 * @param bo
	 * @return  
	 * @see com.hyts.bmsp.record.AbstractRecordDao#selectByConditionToList(java.lang.Object)  
	 */  
	@Override
	public List<Record> selectByConditionToList(BO bo) {
		if(Objects.isNull(bo) || bo instanceof BaseQuery)
			return null;
		BaseQuery query = (BaseQuery)bo;
		return Db.find(query.getSql(), query.getParams());
	}

	/* (非 Javadoc)  
	 * <p>Title: selectByConditionToPage</p>  
	 * <p>Description: </p>  
	 * @param bo
	 * @return  
	 * @see com.hyts.bmsp.record.AbstractRecordDao#selectByConditionToPage(java.lang.Object)  
	 */  
	@Override
	public Page<Record> selectByConditionToPage(BO bo) {
		if(Objects.isNull(bo) || bo instanceof PageQuery)
			return null;
		PageQuery query = (PageQuery)bo;
		return Db.paginate(query.getPageNo(), query.getPageSize(),query.isGroup(),query.getSqlPrefix(),query.getSqlSubfix());
	}

	/* (非 Javadoc)  
	 * <p>Title: selectByConditionToNumber</p>  
	 * <p>Description: </p>  
	 * @param bo
	 * @return  
	 * @see com.hyts.bmsp.record.AbstractRecordDao#selectByConditionToNumber(java.lang.Object)  
	 */  
	@Override
	public int selectByConditionToNumber(BO bo) {
		if(Objects.isNull(bo) || bo instanceof BaseQuery)
			return 0;
		BaseQuery query = (BaseQuery)bo;
		return Db.queryInt(query.getSql(),query.getParams());
	}
	
}
